home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / widgets-mesa / DEMOS / TEA.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-03  |  23.6 KB  |  744 lines

  1. /* tea.c -- demo program for the MesaWS widget
  2.    Copyright (C) 1995 Thorsten.Ohl @ Physik.TH-Darmstadt.de
  3.  
  4.    Parts Copyright (c) Mark J. Kilgard, 1994.  See below.
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.    $Id: tea.c,v 1.6 1996/09/27 10:21:53 ohl Exp $
  21.  
  22.  */
  23.  
  24. /* No 3D graphics package is complete without a teapot demo.
  25.    Use the cursor keys (and other MesaWorkstationWidget translations)
  26.    to move the teapot around.
  27.  
  28.    FIXME: toggling the radiobuttons sends two expose events, which is
  29.           a nuisance on affortable hardware ...
  30.  */
  31.  
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <stdarg.h>
  35. #include <math.h>
  36. #ifndef M_PI
  37. #define M_PI 3.14159265358979323846
  38. #endif
  39. #include <X11/X.h>
  40. #include <X11/Intrinsic.h>
  41. #include <X11/StringDefs.h>
  42. #include <X11/Shell.h>
  43. #include <X11/Xaw/Command.h>
  44. #include <X11/Xaw/Form.h>
  45. #include <X11/Xaw/Toggle.h>
  46. #include <GL/xmesa.h>
  47. #include <GL/gl.h>
  48. #include <GL/glu.h>
  49. #include <GL/glx.h>
  50. #include <GL/MesaWorkstation.h>
  51.  
  52. #ifdef __GLX_MOTIF
  53. #include <GL/MesaMWorkstation.h>
  54. #include <Xm/Form.h>
  55. #include <Xm/RowColumn.h>
  56. #include <Xm/ToggleB.h>
  57. #include <Xm/PushB.h>
  58. #define GLwMakeCurrent GLwMMakeCurrent
  59. #define GLwPostObject           GLwMPostObject
  60. #define GLwBeginView           GLwMBeginView
  61. #define GLwEndView           GLwMEndView
  62. #define GLwSetPolarView           GLwMSetPolarView
  63. #define GLwBeginProjection       GLwMBeginProjection
  64. #define GLwEndProjection       GLwMEndProjection
  65. #define GLwSetFrustumProjection       GLwMSetFrustumProjection
  66. #define GLwGetProjectionList       GLwMGetProjectionList
  67. #define GLwRedrawObjects       GLwMRedrawObjects
  68. #define GLwPostProjection       GLwMPostProjection
  69. #endif /* __GLX_MOTIF */
  70.  
  71. static char *RCS_Id =
  72. "@(#) $Id: tea.c,v 1.6 1996/09/27 10:21:53 ohl Exp $";
  73.  
  74. void glutSolidTeapot (GLdouble scale);
  75. void glutWireTeapot (GLdouble scale);
  76.  
  77. static GLint
  78. alloc_color (Widget w, Colormap cmap, int red, int green, int blue)
  79.   XColor xcolor;
  80.   xcolor.red = red;
  81.   xcolor.green = green;
  82.   xcolor.blue = blue;
  83.   xcolor.flags = DoRed | DoGreen | DoBlue;
  84.   if (!XAllocColor (XtDisplay (w), cmap, &xcolor))
  85.     {
  86.       printf ("Couldn't allocate color!\n");
  87.       exit (1);
  88.     }
  89.   return xcolor.pixel;
  90. }
  91.  
  92. /* This is rather inefficient, but we don't mind for the moment,
  93.    because it works.  */
  94.  
  95. static void
  96. translate_pixels (Widget to, Widget from, ...)
  97. {
  98.   va_list ap;
  99.   char *name;
  100.   Colormap from_cmap, to_cmap;
  101.   XColor xcolor;
  102.  
  103.   XtVaGetValues (from, XtNcolormap, &from_cmap, NULL);
  104.   XtVaGetValues (to, XtNcolormap, &to_cmap, NULL);
  105.  
  106.   va_start (ap, from);
  107.   for (name = va_arg (ap, char *); name != NULL; name = va_arg (ap, char *))
  108.     {
  109.       XtVaGetValues (from, name, &xcolor.pixel, NULL);
  110.       XQueryColor (XtDisplay (from), from_cmap, &xcolor);
  111.       if (!XAllocColor (XtDisplay (to), to_cmap, &xcolor))
  112.     XtAppWarning (XtWidgetToApplicationContext (to),
  113.               "Couldn't allocate color!\n");
  114.       else
  115.     XtVaSetValues (from, name, xcolor.pixel, NULL);
  116.     }
  117.   va_end (ap);
  118. }
  119. #if 0
  120. static Widget
  121. create_command (Widget parent, char *name, XtCallbackProc cb)
  122. {
  123.   Widget ok;
  124.   ok = XtVaCreateManagedWidget (name, commandWidgetClass, parent, NULL);
  125.   XtAddCallback (ok, XtNcallback, cb, NULL);
  126.   return ok;
  127. }
  128. #endif
  129. GLuint light, material;
  130. Widget mesa;
  131.  
  132. void
  133. setup_light (void)
  134. {
  135.   GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0};
  136.   GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
  137.   GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
  138.  
  139.   /* light_position is NOT default value */
  140.   GLfloat light_position[] = {1.0, 0.0, 0.0, 0.0};
  141.   GLfloat global_ambient[] = {0.75, 0.75, 0.75, 1.0};
  142.  
  143.   glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
  144.   glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  145.   glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
  146.   glLightfv (GL_LIGHT0, GL_POSITION, light_position);
  147.  
  148.   glLightModelfv (GL_LIGHT_MODEL_AMBIENT, global_ambient);
  149. }
  150.  
  151. static XtTranslations toggle_translations;
  152. static char toggle_translation_string[] =
  153.   "<Btn1Down>,<Btn1Up>: set() notify()";
  154.  
  155. enum light_source { FIX_OBJ, FIX_OBS };
  156.   
  157. static void
  158. light_cb (Widget w, XtPointer client_data, XtPointer junk)
  159. {
  160.   switch ((enum light_source) client_data)
  161.     {
  162.     case FIX_OBJ:
  163.       /* Fixed wrt object: perform the light setup in the the LIGHT list.  */
  164.       glNewList (light, GL_COMPILE);
  165.         setup_light ();
  166.       glEndList ();
  167.       break;
  168.     case FIX_OBS:
  169.       /* Fixed wrt observer: nuke the LIGHT list and setup the
  170.      light in the default coordinate system.  */
  171.       glNewList (light, GL_COMPILE);
  172.       glEndList ();
  173.       glPushMatrix ();
  174.         glLoadIdentity ();
  175.     setup_light ();
  176.       glPopMatrix ();
  177.       break;
  178.     }
  179.   GLwRedrawObjects (mesa);
  180. }
  181.  
  182. enum material_mode { MAT_GOLD, MAT_RUBY, MAT_EMERALD };
  183.   
  184. static void
  185. material_cb (Widget w, XtPointer client_data, XtPointer junk)
  186. {
  187.   GLfloat gold_ambient[] = {0.24725, 0.1995, 0.0745, 1.0};
  188.   GLfloat gold_diffuse[] = {0.75164, 0.60648, 0.22648, 1.0};
  189.   GLfloat gold_specular[] = {0.628281, 0.555802, 0.366065, 1.0};
  190.   GLfloat gold_shine = 0.4;
  191.   
  192.   GLfloat ruby_ambient[] = {0.1745, 0.01175, 0.01175, 1.0};
  193.   GLfloat ruby_diffuse[] = {0.61424, 0.04136, 0.04136, 1.0};
  194.   GLfloat ruby_specular[] = {0.727811, 0.626959, 0.626959, 1.0};
  195.   GLfloat ruby_shine = 0.6;
  196.   
  197.   GLfloat emerald_ambient[] = {0.0215, 0.1745, 0.0215, 1.0};
  198.   GLfloat emerald_diffuse[] = {0.07568, 0.61424, 0.07568, 1.0};
  199.   GLfloat emerald_specular[] = {0.633, 0.727811, 0.633, 1.0};
  200.   GLfloat emerald_shine = 0.6;
  201.   
  202.   glNewList (material, GL_COMPILE);
  203.   {
  204.     switch ((enum material_mode) client_data)
  205.       {
  206.       case MAT_GOLD:
  207.     glMaterialfv (GL_FRONT, GL_AMBIENT, gold_ambient);
  208.     glMaterialfv (GL_FRONT, GL_DIFFUSE, gold_diffuse);
  209.     glMaterialfv (GL_FRONT, GL_SPECULAR, gold_specular);
  210.     glMaterialf (GL_FRONT, GL_SHININESS, gold_shine*128.0);
  211.     break;
  212.       case MAT_RUBY:
  213.     glMaterialfv (GL_FRONT, GL_AMBIENT, ruby_ambient);
  214.     glMaterialfv (GL_FRONT, GL_DIFFUSE, ruby_diffuse);
  215.     glMaterialfv (GL_FRONT, GL_SPECULAR, ruby_specular);
  216.     glMaterialf (GL_FRONT, GL_SHININESS, ruby_shine*128.0);
  217.     break;
  218.       case MAT_EMERALD:
  219.     glMaterialfv (GL_FRONT, GL_AMBIENT, emerald_ambient);
  220.     glMaterialfv (GL_FRONT, GL_DIFFUSE, emerald_diffuse);
  221.     glMaterialfv (GL_FRONT, GL_SPECULAR, emerald_specular);
  222.     glMaterialf (GL_FRONT, GL_SHININESS, emerald_shine*128.0);
  223.     break;
  224.       }
  225.   }
  226.   glEndList ();
  227.   GLwRedrawObjects (mesa);
  228. }
  229.  
  230. void
  231. quit_function (Widget w, XtPointer closure, XtPointer call_data)
  232. {
  233.   exit (0);
  234. }
  235.  
  236. static String fallback_resources[] =
  237. {
  238. #ifndef __GLX_MOTIF
  239.   "*MesaWorkstation.debug: false",
  240.   "*MesaWorkstation.installColormap: true",
  241.   "*MesaWorkstation.ximage: true",
  242.   "*MesaWorkstation.doublebuffer: false",
  243.   "*MesaWorkstation.depthSize: 1",
  244. #else /* __GLX_MOTIF */
  245.   "*MesaMWorkstation.debug: false",
  246.   "*MesaMWorkstation.installColormap: true",
  247.   "*MesaMWorkstation.ximage: true",
  248.   "*MesaMWorkstation.doublebuffer: false",
  249. #endif /* __GLX_MOTIF */
  250.   "*quit.label: Exit",
  251.   "*mesa.width: 400",
  252.   "*mesa.height: 400",
  253.   NULL
  254. };
  255.  
  256. int
  257. main (int argc, char *argv[])
  258. {
  259.   Widget top, frame, quit;
  260.   Widget fix_obj, fix_obs;
  261.   Widget gold, ruby, emerald;
  262. #ifdef __GLX_MOTIF
  263.   Widget rb1, rb2;
  264. #endif
  265.   XtAppContext app_context;
  266.   Boolean cmap_installed;
  267.   GLuint teapot;
  268.  
  269.   XtSetLanguageProc (NULL, NULL, NULL);
  270.   top = XtVaAppInitialize (&app_context, "Tea", NULL, 0,
  271.                &argc, argv, fallback_resources, NULL);
  272.  
  273. #ifdef __GLX_MOTIF
  274.   frame = XtVaCreateManagedWidget ("frame", xmFormWidgetClass,
  275.                    top,
  276.                    NULL);
  277.  
  278.   mesa = XtVaCreateManagedWidget ("mesa", mesaMWorkstationWidgetClass,
  279.                   frame,
  280.                   GLwNrgba, True,
  281.                   XmNtopAttachment, XmATTACH_FORM,
  282.                   XmNtopOffset, 10,
  283.                   XmNleftAttachment, XmATTACH_FORM,
  284.                   XmNleftOffset, 10,
  285.                   XmNrightAttachment, XmATTACH_NONE,
  286.                   XmNrightOffset, 10,
  287.                   XmNbottomAttachment, XmATTACH_FORM,
  288.                   NULL);
  289.   rb1 = XtVaCreateManagedWidget ("rb1", xmRowColumnWidgetClass,
  290.                  frame,
  291.                  XmNradioBehavior, True,
  292.                  XmNtopAttachment, XmATTACH_FORM,
  293.                  XmNtopOffset, 10,
  294.                  XmNleftAttachment, XmATTACH_WIDGET,
  295.                  XmNleftOffset, 10,
  296.                  XmNleftWidget, mesa,
  297.                  XmNrightAttachment, XmATTACH_NONE,
  298.                  XmNbottomAttachment, XmATTACH_NONE,
  299.                  NULL);
  300.   fix_obj = XtVaCreateManagedWidget ("fix_obj", xmToggleButtonWidgetClass,
  301.                      rb1,
  302.                      XtNstate, False,
  303.                      XmNtopAttachment, XmATTACH_FORM,
  304.                      XmNtopOffset, 0,
  305.                      XmNleftAttachment, XmATTACH_FORM,
  306.                      XmNleftOffset, 0,
  307.                      XmNrightAttachment, XmATTACH_FORM,
  308.                      XmNrightOffset, 0,
  309.                      XmNbottomAttachment, XmATTACH_FORM,
  310.                      XmNbottomOffset, 0,
  311.                      NULL);
  312.   fix_obs = XtVaCreateManagedWidget ("fix_obs", xmToggleButtonWidgetClass,
  313.                      rb1,
  314.                      XtNstate, True,
  315.                      XmNtopAttachment, XmATTACH_WIDGET,
  316.                      XmNtopOffset, 10,
  317.                      XmNtopWidget, fix_obj,
  318.                      XmNleftAttachment, XmATTACH_FORM,
  319.                      XmNleftOffset, 0,
  320.                      XmNrightAttachment, XmATTACH_FORM,
  321.                      XmNrightOffset, 0,
  322.                      XmNbottomAttachment, XmATTACH_FORM,
  323.                      XmNbottomOffset, 0,
  324.                      NULL);
  325. #else /* __GLX_MOTIF */
  326.   frame = XtVaCreateManagedWidget ("frame", formWidgetClass,
  327.                    top,
  328.                    NULL);
  329.  
  330.   mesa = XtVaCreateManagedWidget ("mesa", mesaWorkstationWidgetClass,
  331.                   frame,
  332.                   GLwNrgba, True,
  333.                   NULL);
  334.   fix_obj = XtVaCreateManagedWidget ("fix_obj", toggleWidgetClass,
  335.                      frame,
  336.                      XtNlabel, "light fixed wrt object",
  337.                      XtNstate, False,
  338.                      XtNfromHoriz, mesa, XtNhorizDistance, 10,
  339.                      NULL);
  340.   fix_obs = XtVaCreateManagedWidget ("fix_obs", toggleWidgetClass,
  341.                      frame,
  342.                      XtNlabel, "light fixed wrt observer",
  343.                      XtNstate, True,
  344.                      XtNradioGroup, fix_obj,
  345.                      XtNfromVert, fix_obj, XtNvertDistance, 0,
  346.                      XtNfromHoriz, mesa, XtNhorizDistance, 10,
  347.                      NULL);
  348. #endif /* __GLX_MOTIF */
  349. #ifdef __GLX_MOTIF
  350.   XtAddCallback (fix_obj, XmNarmCallback, light_cb, (XtPointer) FIX_OBJ);
  351.   XtAddCallback (fix_obs, XmNarmCallback, light_cb, (XtPointer) FIX_OBS);
  352. #else /* __GLX_MOTIF */
  353.   toggle_translations = XtParseTranslationTable (toggle_translation_string);
  354.   XtOverrideTranslations (fix_obj, toggle_translations);
  355.   XtOverrideTranslations (fix_obs, toggle_translations);
  356.   XtAddCallback (fix_obj, XtNcallback, light_cb, (XtPointer) FIX_OBJ);
  357.   XtAddCallback (fix_obs, XtNcallback, light_cb, (XtPointer) FIX_OBS);
  358. #endif /* __GLX_MOTIF */
  359.  
  360. #ifdef __GLX_MOTIF
  361.   rb2 = XtVaCreateManagedWidget ("rb2", xmRowColumnWidgetClass,
  362.                  frame,
  363.                  XmNradioBehavior, True,
  364.                  XmNtopAttachment, XmATTACH_WIDGET,
  365.                  XmNtopOffset, 20,
  366.                  XmNtopWidget, rb1,
  367.                  XmNleftAttachment, XmATTACH_WIDGET,
  368.                  XmNleftOffset, 10,
  369.                  XmNleftWidget, mesa,
  370.                  XmNrightAttachment, XmATTACH_NONE,
  371.                  XmNbottomAttachment, XmATTACH_NONE,
  372.                  NULL);
  373.   gold = XtVaCreateManagedWidget ("gold", xmToggleButtonWidgetClass,
  374.                   rb2,
  375.                   XtNstate, True,
  376.                   XmNtopAttachment, XmATTACH_FORM,
  377.                   XmNtopOffset, 0,
  378.                   XmNleftAttachment, XmATTACH_FORM,
  379.                   XmNleftOffset, 0,
  380.                   XmNrightAttachment, XmATTACH_NONE,
  381.                   XmNbottomAttachment, XmATTACH_NONE,
  382.                   NULL);
  383.   ruby = XtVaCreateManagedWidget ("ruby", xmToggleButtonWidgetClass,
  384.                   rb2,
  385.                   XtNstate, False,
  386.                   XmNtopAttachment, XmATTACH_WIDGET,
  387.                   XmNtopOffset, 0,
  388.                   XmNtopWidget, gold,
  389.                   XmNleftAttachment, XmATTACH_FORM,
  390.                   XmNleftOffset, 0,
  391.                   XmNrightAttachment, XmATTACH_NONE,
  392.                   XmNbottomAttachment, XmATTACH_NONE,
  393.                   NULL);
  394.   emerald = XtVaCreateManagedWidget ("emerald", xmToggleButtonWidgetClass,
  395.                      rb2,
  396.                      XtNstate, False,
  397.                      XmNtopAttachment, XmATTACH_WIDGET,
  398.                      XmNtopOffset, 0,
  399.                      XmNtopWidget, ruby,
  400.                      XmNleftAttachment, XmATTACH_FORM,
  401.                      XmNleftOffset, 0,
  402.                      XmNrightAttachment, XmATTACH_NONE,
  403.                      XmNbottomAttachment, XmATTACH_NONE,
  404.                      NULL);
  405. #else /* __GLX_MOTIF */
  406.   gold = XtVaCreateManagedWidget ("gold", toggleWidgetClass,
  407.                   frame,
  408.                   XtNlabel, "gold",
  409.                   XtNstate, True,
  410.                   XtNfromVert, fix_obs, XtNvertDistance, 20,
  411.                   XtNfromHoriz, mesa, XtNhorizDistance, 10,
  412.                   NULL);
  413.   ruby = XtVaCreateManagedWidget ("ruby", toggleWidgetClass,
  414.                   frame,
  415.                   XtNlabel, "ruby",
  416.                   XtNstate, False,
  417.                   XtNradioGroup, gold,
  418.                   XtNfromVert, gold, XtNvertDistance, 0,
  419.                   XtNfromHoriz, mesa, XtNhorizDistance, 10,
  420.                   NULL);
  421.   emerald = XtVaCreateManagedWidget ("emerald", toggleWidgetClass,
  422.                      frame,
  423.                      XtNlabel, "emerald",
  424.                      XtNstate, False,
  425.                      XtNradioGroup, gold,
  426.                      XtNfromVert, ruby, XtNvertDistance, 0,
  427.                      XtNfromHoriz, mesa, XtNhorizDistance, 10,
  428.                      NULL);
  429. #endif /* __GLX_MOTIF */
  430.  
  431. #ifdef __GLX_MOTIF
  432.   XtAddCallback (gold, XmNarmCallback, material_cb, (XtPointer) MAT_GOLD);
  433.   XtAddCallback (ruby, XmNarmCallback, material_cb, (XtPointer) MAT_RUBY);
  434.   XtAddCallback (emerald, XmNarmCallback, material_cb, (XtPointer) MAT_EMERALD);
  435. #else /* __GLX_MOTIF */
  436.   XtOverrideTranslations (gold, toggle_translations);
  437.   XtOverrideTranslations (ruby, toggle_translations);
  438.   XtOverrideTranslations (emerald, toggle_translations);
  439.   XtAddCallback (gold, XtNcallback, material_cb, (XtPointer) MAT_GOLD);
  440.   XtAddCallback (ruby, XtNcallback, material_cb, (XtPointer) MAT_RUBY);
  441.   XtAddCallback (emerald, XtNcallback, material_cb, (XtPointer) MAT_EMERALD);
  442. #endif /* __GLX_MOTIF */
  443.  
  444. #ifdef __GLX_MOTIF
  445.   quit = XtVaCreateManagedWidget ("quit", xmPushButtonWidgetClass,
  446.                   frame,
  447.                   XmNtopAttachment, XmATTACH_WIDGET,
  448.                   XmNtopWidget, rb2,
  449.                   XmNtopOffset, 50,
  450.                   XmNleftAttachment, XmATTACH_WIDGET,
  451.                   XmNleftOffset, 10,
  452.                   XmNleftWidget, mesa,
  453.                   XmNrightAttachment, XmATTACH_NONE,
  454.                   XmNrightOffset, 10,
  455.                   XmNbottomAttachment, XmATTACH_NONE,
  456.                   NULL);
  457.   XtAddCallback (quit, XmNarmCallback, quit_function, NULL);
  458. #else /* __GLX_MOTIF */
  459.   quit = XtVaCreateManagedWidget ("quit", commandWidgetClass,
  460.                   frame,
  461.                   XtNfromVert, emerald, XtNvertDistance, 50,
  462.                   XtNfromHoriz, mesa, XtNhorizDistance, 10,
  463.                   NULL);
  464.   XtAddCallback (quit, XtNcallback, quit_function, NULL);
  465. #endif /* __GLX_MOTIF */
  466.  
  467.   XtRealizeWidget (top);
  468.  
  469.   XtVaGetValues (mesa, GLwNinstallColormap, &cmap_installed, NULL);
  470.   if (cmap_installed)
  471.     {
  472.       translate_pixels (mesa, quit,
  473.             XtNbackground, XtNforeground, XtNborder, NULL);
  474.       translate_pixels (mesa, fix_obj,
  475.             XtNbackground, XtNforeground, XtNborder, NULL);
  476.       translate_pixels (mesa, fix_obs,
  477.             XtNbackground, XtNforeground, XtNborder, NULL);
  478.       translate_pixels (mesa, gold,
  479.             XtNbackground, XtNforeground, XtNborder, NULL);
  480.       translate_pixels (mesa, ruby,
  481.             XtNbackground, XtNforeground, XtNborder, NULL);
  482.       translate_pixels (mesa, emerald,
  483.             XtNbackground, XtNforeground, XtNborder, NULL);
  484.       translate_pixels (mesa, frame, XtNbackground, XtNborder, NULL);
  485.       XWarpPointer (XtDisplay (mesa), None, XtWindow (mesa),
  486.             0, 0, 0, 0, 0, 0);
  487.     }
  488.  
  489.   GLwMakeCurrent (mesa);
  490.  
  491.   glFrontFace (GL_CW);
  492.   glEnable (GL_LIGHTING);
  493.   glEnable (GL_LIGHT0);
  494.   glEnable (GL_AUTO_NORMAL);
  495.   glEnable (GL_NORMALIZE);
  496.   glEnable (GL_DEPTH_TEST);
  497.   glDepthFunc (GL_LESS);
  498.  
  499.   GLwSetFrustumProjection (mesa, -1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
  500.   GLwSetPolarView (mesa, 3.0, 2*M_PI/3, -M_PI/3);
  501.  
  502.   teapot = glGenLists (1);
  503.   glNewList (teapot, GL_COMPILE);
  504.   {
  505.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  506.     glutSolidTeapot (1.0);
  507.   }
  508.   glEndList ();
  509.  
  510.   light = glGenLists (1);
  511.   glNewList (light, GL_COMPILE);
  512.   glEndList ();
  513.  
  514.   material = glGenLists (1);
  515.   glNewList (material, GL_COMPILE);
  516.   glEndList ();
  517.  
  518.   material_cb (NULL, (XtPointer) MAT_GOLD, NULL);
  519.   light_cb (NULL, (XtPointer) FIX_OBS, NULL);
  520.  
  521.   GLwPostObject (mesa, light);
  522.   GLwPostObject (mesa, material);
  523.   GLwPostObject (mesa, teapot);
  524.  
  525.   XtAppMainLoop (app_context);
  526.   return (0);
  527. }
  528.  
  529.  
  530. /* Copyright (c) Mark J. Kilgard, 1994. */
  531.  
  532. /**
  533. (c) Copyright 1993, Silicon Graphics, Inc.
  534.  
  535. ALL RIGHTS RESERVED
  536.  
  537. Permission to use, copy, modify, and distribute this software
  538. for any purpose and without fee is hereby granted, provided
  539. that the above copyright notice appear in all copies and that
  540. both the copyright notice and this permission notice appear in
  541. supporting documentation, and that the name of Silicon
  542. Graphics, Inc. not be used in advertising or publicity
  543. pertaining to distribution of the software without specific,
  544. written prior permission.
  545.  
  546. THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU
  547. "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR
  548. OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
  549. MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  IN NO
  550. EVENT SHALL SILICON GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE
  551. ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR
  552. CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER,
  553. INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE,
  554. SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR
  555. NOT SILICON GRAPHICS, INC.  HAS BEEN ADVISED OF THE POSSIBILITY
  556. OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  557. ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE OR
  558. PERFORMANCE OF THIS SOFTWARE.
  559.  
  560. US Government Users Restricted Rights
  561.  
  562. Use, duplication, or disclosure by the Government is subject to
  563. restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  564. (c)(1)(ii) of the Rights in Technical Data and Computer
  565. Software clause at DFARS 252.227-7013 and/or in similar or
  566. successor clauses in the FAR or the DOD or NASA FAR
  567. Supplement.  Unpublished-- rights reserved under the copyright
  568. laws of the United States.  Contractor/manufacturer is Silicon
  569. Graphics, Inc., 2011 N.  Shoreline Blvd., Mountain View, CA
  570. 94039-7311.
  571.  
  572. OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  573. */
  574.  
  575. #include <GL/gl.h>
  576. /* #include <GL/glut.h> */
  577.  
  578. /* Rim, body, lid, and bottom data must be reflected in x
  579.    and y; handle and spout data across the y axis only.  */
  580.  
  581. long patchdata[][16] =
  582. {
  583.     /* rim */
  584.   {102, 103, 104, 105, 4, 5, 6, 7, 8, 9, 10, 11,
  585.     12, 13, 14, 15},
  586.     /* body */
  587.   {12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
  588.     24, 25, 26, 27},
  589.   {24, 25, 26, 27, 29, 30, 31, 32, 33, 34, 35, 36,
  590.     37, 38, 39, 40},
  591.     /* lid */
  592.   {96, 96, 96, 96, 97, 98, 99, 100, 101, 101, 101,
  593.     101, 0, 1, 2, 3,},
  594.   {0, 1, 2, 3, 106, 107, 108, 109, 110, 111, 112,
  595.     113, 114, 115, 116, 117},
  596.     /* bottom */
  597.   {118, 118, 118, 118, 124, 122, 119, 121, 123, 126,
  598.     125, 120, 40, 39, 38, 37},
  599.     /* handle */
  600.   {41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52,
  601.     53, 54, 55, 56},
  602.   {53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
  603.     28, 65, 66, 67},
  604.     /* spout */
  605.   {68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  606.     80, 81, 82, 83},
  607.   {80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
  608.     92, 93, 94, 95}
  609. };
  610. /* *INDENT-OFF* */
  611.  
  612. float cpdata[][3] =
  613. {
  614.     {0.2, 0, 2.7}, {0.2, -0.112, 2.7}, {0.112, -0.2, 2.7}, {0,
  615.     -0.2, 2.7}, {1.3375, 0, 2.53125}, {1.3375, -0.749, 2.53125},
  616.     {0.749, -1.3375, 2.53125}, {0, -1.3375, 2.53125}, {1.4375,
  617.     0, 2.53125}, {1.4375, -0.805, 2.53125}, {0.805, -1.4375,
  618.     2.53125}, {0, -1.4375, 2.53125}, {1.5, 0, 2.4}, {1.5, -0.84,
  619.     2.4}, {0.84, -1.5, 2.4}, {0, -1.5, 2.4}, {1.75, 0, 1.875},
  620.     {1.75, -0.98, 1.875}, {0.98, -1.75, 1.875}, {0, -1.75,
  621.     1.875}, {2, 0, 1.35}, {2, -1.12, 1.35}, {1.12, -2, 1.35},
  622.     {0, -2, 1.35}, {2, 0, 0.9}, {2, -1.12, 0.9}, {1.12, -2,
  623.     0.9}, {0, -2, 0.9}, {-2, 0, 0.9}, {2, 0, 0.45}, {2, -1.12,
  624.     0.45}, {1.12, -2, 0.45}, {0, -2, 0.45}, {1.5, 0, 0.225},
  625.     {1.5, -0.84, 0.225}, {0.84, -1.5, 0.225}, {0, -1.5, 0.225},
  626.     {1.5, 0, 0.15}, {1.5, -0.84, 0.15}, {0.84, -1.5, 0.15}, {0,
  627.     -1.5, 0.15}, {-1.6, 0, 2.025}, {-1.6, -0.3, 2.025}, {-1.5,
  628.     -0.3, 2.25}, {-1.5, 0, 2.25}, {-2.3, 0, 2.025}, {-2.3, -0.3,
  629.     2.025}, {-2.5, -0.3, 2.25}, {-2.5, 0, 2.25}, {-2.7, 0,
  630.     2.025}, {-2.7, -0.3, 2.025}, {-3, -0.3, 2.25}, {-3, 0,
  631.     2.25}, {-2.7, 0, 1.8}, {-2.7, -0.3, 1.8}, {-3, -0.3, 1.8},
  632.     {-3, 0, 1.8}, {-2.7, 0, 1.575}, {-2.7, -0.3, 1.575}, {-3,
  633.     -0.3, 1.35}, {-3, 0, 1.35}, {-2.5, 0, 1.125}, {-2.5, -0.3,
  634.     1.125}, {-2.65, -0.3, 0.9375}, {-2.65, 0, 0.9375}, {-2,
  635.     -0.3, 0.9}, {-1.9, -0.3, 0.6}, {-1.9, 0, 0.6}, {1.7, 0,
  636.     1.425}, {1.7, -0.66, 1.425}, {1.7, -0.66, 0.6}, {1.7, 0,
  637.     0.6}, {2.6, 0, 1.425}, {2.6, -0.66, 1.425}, {3.1, -0.66,
  638.     0.825}, {3.1, 0, 0.825}, {2.3, 0, 2.1}, {2.3, -0.25, 2.1},
  639.     {2.4, -0.25, 2.025}, {2.4, 0, 2.025}, {2.7, 0, 2.4}, {2.7,
  640.     -0.25, 2.4}, {3.3, -0.25, 2.4}, {3.3, 0, 2.4}, {2.8, 0,
  641.     2.475}, {2.8, -0.25, 2.475}, {3.525, -0.25, 2.49375},
  642.     {3.525, 0, 2.49375}, {2.9, 0, 2.475}, {2.9, -0.15, 2.475},
  643.     {3.45, -0.15, 2.5125}, {3.45, 0, 2.5125}, {2.8, 0, 2.4},
  644.     {2.8, -0.15, 2.4}, {3.2, -0.15, 2.4}, {3.2, 0, 2.4}, {0, 0,
  645.     3.15}, {0.8, 0, 3.15}, {0.8, -0.45, 3.15}, {0.45, -0.8,
  646.     3.15}, {0, -0.8, 3.15}, {0, 0, 2.85}, {1.4, 0, 2.4}, {1.4,
  647.     -0.784, 2.4}, {0.784, -1.4, 2.4}, {0, -1.4, 2.4}, {0.4, 0,
  648.     2.55}, {0.4, -0.224, 2.55}, {0.224, -0.4, 2.55}, {0, -0.4,
  649.     2.55}, {1.3, 0, 2.55}, {1.3, -0.728, 2.55}, {0.728, -1.3,
  650.     2.55}, {0, -1.3, 2.55}, {1.3, 0, 2.4}, {1.3, -0.728, 2.4},
  651.     {0.728, -1.3, 2.4}, {0, -1.3, 2.4}, {0, 0, 0}, {1.425,
  652.     -0.798, 0}, {1.5, 0, 0.075}, {1.425, 0, 0}, {0.798, -1.425,
  653.     0}, {0, -1.5, 0.075}, {0, -1.425, 0}, {1.5, -0.84, 0.075},
  654.     {0.84, -1.5, 0.075}
  655. };
  656.  
  657. static float tex[2][2][2] =
  658. {
  659.   { {0, 0},
  660.     {1, 0}},
  661.   { {0, 1},
  662.     {1, 1}}
  663. };
  664.  
  665. /* *INDENT-ON* */
  666.  
  667. static void
  668. teapot(GLint grid, GLdouble scale, GLenum type)
  669. {
  670.   float p[4][4][3], q[4][4][3], r[4][4][3], s[4][4][3];
  671.   long i, j, k, l;
  672.  
  673.   glPushAttrib(GL_ENABLE_BIT | GL_EVAL_BIT);
  674.   glEnable(GL_AUTO_NORMAL);
  675.   glEnable(GL_NORMALIZE);
  676.   glEnable(GL_MAP2_VERTEX_3);
  677.   glEnable(GL_MAP2_TEXTURE_COORD_2);
  678.   glPushMatrix();
  679.   /* glRotatef(270.0, 1.0, 0.0, 0.0); */
  680.   /* Rotate it upwards in the standard MesaWorkstation
  681.      coordinate system.  */
  682.   glRotatef(90.0, 0.0, 1.0, 0.0);
  683.   glRotatef(90.0, 1.0, 0.0, 0.0);
  684.   glScalef(0.5 * scale, 0.5 * scale, 0.5 * scale);
  685.   glTranslatef(0.0, 0.0, -1.5);
  686.   for (i = 0; i < 10; i++) {
  687.     for (j = 0; j < 4; j++) {
  688.       for (k = 0; k < 4; k++) {
  689.         for (l = 0; l < 3; l++) {
  690.           p[j][k][l] = cpdata[patchdata[i][j * 4 + k]][l];
  691.           q[j][k][l] = cpdata[patchdata[i][j * 4 + (3 - k)]][l];
  692.           if (l == 1)
  693.             q[j][k][l] *= -1.0;
  694.           if (i < 6) {
  695.             r[j][k][l] =
  696.               cpdata[patchdata[i][j * 4 + (3 - k)]][l];
  697.             if (l == 0)
  698.               r[j][k][l] *= -1.0;
  699.             s[j][k][l] = cpdata[patchdata[i][j * 4 + k]][l];
  700.             if (l == 0)
  701.               s[j][k][l] *= -1.0;
  702.             if (l == 1)
  703.               s[j][k][l] *= -1.0;
  704.           }
  705.         }
  706.       }
  707.     }
  708.     glMap2f(GL_MAP2_TEXTURE_COORD_2, 0, 1, 2, 2, 0, 1, 4, 2,
  709.       &tex[0][0][0]);
  710.     glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4,
  711.       &p[0][0][0]);
  712.     glMapGrid2f(grid, 0.0, 1.0, grid, 0.0, 1.0);
  713.     glEvalMesh2(type, 0, grid, 0, grid);
  714.     glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4,
  715.       &q[0][0][0]);
  716.     glEvalMesh2(type, 0, grid, 0, grid);
  717.     if (i < 6) {
  718.       glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4,
  719.         &r[0][0][0]);
  720.       glEvalMesh2(type, 0, grid, 0, grid);
  721.       glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4, 0, 1, 12, 4,
  722.         &s[0][0][0]);
  723.       glEvalMesh2(type, 0, grid, 0, grid);
  724.     }
  725.   }
  726.   glPopMatrix();
  727.   glPopAttrib();
  728. }
  729.  
  730. /* CENTRY */
  731. void
  732. glutSolidTeapot(GLdouble scale)
  733. {
  734.   teapot(14, scale, GL_FILL);
  735. }
  736.  
  737. void
  738. glutWireTeapot(GLdouble scale)
  739. {
  740.   teapot(10, scale, GL_LINE);
  741. }
  742. /* ENDCENTRY */
  743.